<?php
declare(strict_types=1);

namespace App\Services\RoomScore;



use App\Models\MainDB\RoomConnectionRecord;
use App\Models\MainDB\UserData;
use App\Services\RoomScore\RoomManagerServer;

class RoomUserServer
{

    /**
     * 房间用户信息, 并非房间在线用户
     * @param int $roomId
     * @param array $userIds
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function users(int $roomId, array $userIds): array
    {
        if(empty($userIds)){
            return [];
        }
        $userFields = ["user_id", "user_code", "nickname", "head_picture", "sex", "charm_level", "wealth_level"];
        $query = UserData::query();
        $query->whereIn('user_id',$userIds);
        $query->with(['wealth'=>function($query){
            $query->select(['grade as level','title','head_picture as icon']);
        }])->get()->toArray();
        $query->with(["charm"]);
        $query->select($userFields);
        $users = $query->get();

        if($users->isEmpty()){
            return [];
        }

        $res = [];
        // map
        $roomManagerServer = new  RoomManagerServer();
        $userRoleMap = $roomManagerServer->batchUsersRoleMap($roomId, $userIds);
        $userGiftScoreMap = $roomManagerServer->batchUsersScoreMap($roomId, $userIds);
        $userSpecialMap = $roomManagerServer->batchUserSpecialMap($userIds);
        $userGiveGiftCoinMap = $roomManagerServer->batchRoomUsersGiveCoinMap($roomId, $userIds);
        $userTodayGiveGiftCoinMap = $roomManagerServer->batchRoomUsersTodayGiveCoinMap($roomId, $userIds);
        $userConnection = $this->roomConnection($userIds);

        foreach ($users as $user){
            $userId = $user->user_id;
            $res[$user->user_id] = [
                "id" => $user->user_id,
                "code" => $user->user_code,
                "nickname" => $user->nickname,
                "avatar" => $user->head_picture,
                "charm" => [
                    "level" => $user->charm->level ?? 0,
                    "title" => $user->charm->title ?? "",
                    "icon" => $user->charm->icon ?? "",
                ],
                "wealth" => [
                    "level" => $user->wealth->level ?? 0,
                    "title" => $user->wealth->title ?? "",
                    "icon" => $user->wealth->icon ?? "",
                ],
                "nobility" => [
                    "level" => 0,
                    "title" => "",
                    "icon" => "",
                ],
                "gift_score" => (int)($userGiftScoreMap[$userId] ?? 0),
                "give_gift_coin" => (int)($userGiveGiftCoinMap[$userId] ?? 0),
                "today_give_gift_coin" => $userTodayGiveGiftCoinMap[$userId] ?? 0,
                "room_role" => $userRoleMap[$userId], // 房间角色
                "special" => $userSpecialMap[$userId],
                "in_record" => !empty($userConnection[$userId]) ? 1 : 0,
                "sex" => $user->sex,
                "gender" => $user->sex,
            ];
        }
        return $res;
    }

    public function roomConnection($userIds){
        $res = [];
        if(empty($userIds)){
            return $res;
        }
        $anchorConnection = RoomConnectionRecord::query()->where('status', 2)
            ->whereIn('anchor_id', $userIds)
            ->get();
        $userConnection = RoomConnectionRecord::query()->where('status', 2)
            ->whereIn('user_id', $userIds)
            ->get();
        if($userConnection->isEmpty() && $anchorConnection->isEmpty()){
            return $res;
        }
        foreach ($userConnection as $model){
            $res[$model['anchor_id']] = $model['id'];
        }
        foreach ($anchorConnection as $model){
            $res[$model['user_id']] = $model['id'];
        }
        return $res;
    }


}